﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Xml;
using System.Web;
using System.Web.Script.Serialization;

namespace LongNet.Util
{
    public class DataCopy
    {
        /// <summary>
        /// 复制数据行到Model对象方法
        /// </summary>
        /// <typeparam name="T">要赋值的数据对象泛型</typeparam>
        /// <param name="dataRow">数据行</param>
        /// <param name="objModel">要赋值的数据对象</param>
        /// <returns></returns>
        public static bool CopyDataRowToModel<T>(DataRow dataRow, ref T objModel)
        {
            try
            {
                if (dataRow == null)
                {
                    objModel = default(T);

                    return true;
                }
                System.Reflection.PropertyInfo[] m_PropertyInfos = objModel.GetType().GetProperties();

                string strPropertyName = "";

                for (int i = 0; i < m_PropertyInfos.GetLength(0); i++)
                {
                    strPropertyName = m_PropertyInfos[i].Name;

                    if (dataRow.Table.Columns.Contains(strPropertyName) && !Convert.IsDBNull(dataRow[strPropertyName]))
                    {
                        m_PropertyInfos[i].SetValue(objModel, Convert.ChangeType(dataRow[strPropertyName], m_PropertyInfos[i].PropertyType), null);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 复制对象的相同属性到另外一个对象方法
        /// </summary>
        /// <typeparam name="U">源对象类型</typeparam>
        /// <typeparam name="T">目标对象类型</typeparam>
        /// <param name="SourceModel">源对象</param>
        /// <param name="TargetModel">目标对象</param>
        /// <returns></returns>
        public static bool CopyModelToModel<U, T>(U SourceModel, ref T TargetModel)
        {
            try
            {
                if (SourceModel == null)
                {
                    TargetModel = default(T);

                    return true;
                }
                System.Reflection.PropertyInfo[] m_PropertyInfos = SourceModel.GetType().GetProperties();

                string strPropertyName = "";

                for (int i = 0; i < m_PropertyInfos.GetLength(0); i++)
                {
                    strPropertyName = m_PropertyInfos[i].Name;

                    System.Reflection.PropertyInfo m_PropertyInfo = TargetModel.GetType().GetProperty(strPropertyName);

                    if (m_PropertyInfo != null)
                    {
                        try
                        {
                            m_PropertyInfo.SetValue(TargetModel, Convert.ChangeType(m_PropertyInfos[i].GetValue(SourceModel, null), m_PropertyInfos[i].PropertyType), null);
                        }
                        catch
                        { }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 复制Model对象到数据行方法
        /// </summary>
        /// <typeparam name="T">源数据对象泛型</typeparam>
        /// <param name="objModel">源数据对象</param>
        /// <param name="dataRow">要赋值的数据行</param>
        /// <returns></returns>
        public static bool CopyModelToDataRow<T>(ref T objModel, DataRow dataRow)
        {
            return CopyModelToDataRow<T>(ref objModel, dataRow, true);
        }

        /// <summary>
        /// 复制Model对象到数据行方法，如果源数据对象为空，则不复制
        /// </summary>
        /// <typeparam name="T">源数据对象泛型</typeparam>
        /// <param name="objModel">源数据对象</param>
        /// <param name="dataRow">要赋值的数据行</param>
        /// <param name="isCopyNull">是否复制空</param>
        /// <returns></returns>
        public static bool CopyModelToDataRow<T>(ref T objModel, DataRow dataRow, bool isCopyNull)
        {
            try
            {
                if (dataRow == null)
                {
                    objModel = default(T);

                    return true;
                }
                System.Reflection.PropertyInfo[] m_PropertyInfos = objModel.GetType().GetProperties();

                string strPropertyName = "";

                for (int i = 0; i < m_PropertyInfos.GetLength(0); i++)
                {
                    strPropertyName = m_PropertyInfos[i].Name;

                    if (dataRow.Table.Columns.Contains(strPropertyName) && isCopyNull)
                    {
                        dataRow[strPropertyName] = m_PropertyInfos[i].GetValue(objModel, null);

                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     

        /// <summary>
        /// 复制对象数组的某列属性到另外一个数组中方法
        /// </summary>
        /// <Description>复制对象数组的某列属性到另外一个数组中方法 By 彭承龙 2010-05-27</Description>
        /// <typeparam name="T">数组类型</typeparam>
        /// <param name="SourceModel">原对象数组</param>
        /// <param name="Field">属性字段</param>
        /// <returns></returns>
        public static List<T> CopyModelPropertyToList<T>(object[] SourceModel, string Field)
        {
            try
            {
                if (SourceModel == null || SourceModel.Length == 0)
                {
                    return null;
                }
                System.Reflection.PropertyInfo m_PropertyInfo = SourceModel[0].GetType().GetProperty(Field);
                if (m_PropertyInfo != null)
                {
                    List<T> Arrays = new List<T>();
                    foreach (var model in SourceModel)
                    {
                        if (model != null)
                        {
                            T item = (T)model.GetType().GetProperty(Field).GetValue(model, null);
                            Arrays.Add(item);
                        }
                    }

                    return Arrays;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable ConvertJsonToDataTale(string JsonString)
        {
            try
            {   
                JavaScriptSerializer s = new JavaScriptSerializer();                
                object[] JsonData = (object[])s.DeserializeObject(JsonString);
                DataTable jsonDataTable = new DataTable();
                Dictionary<string, object> objs = (Dictionary<string, object>)JsonData[0];
                foreach (var obj in objs)
                {
                    jsonDataTable.Columns.Add(new DataColumn(obj.Key, obj.Value.GetType()));
                }
                foreach (var jon in JsonData)
                {
                    Dictionary<string, object> dics = (Dictionary<string, object>)jon;
                    DataRow dr = jsonDataTable.NewRow();
                    foreach (var dic in dics)
                    {
                        dr[dic.Key] = dic.Value;
                    }
                    jsonDataTable.Rows.Add(dr);
                }
                jsonDataTable.AcceptChanges();
                return jsonDataTable;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
